﻿using DDU.Infrastructure;
using System.Data.Linq.Mapping;
using System.IO.IsolatedStorage;
using System.IO;
using System;

namespace DDU.DataModel
{
    [Table(Name = "T_Books")]
    public class BookInfo : NotificationObject
    {
        #region Fields
        private string _guid = System.Guid.NewGuid().ToString();
        private string _isbn;
        private string _semesterGuid;
        private string _title;
        private string _teacher;
        private string _press;
        private double _price = 0.00;
        private string _image;
        #endregion

        #region Properties
        [Column(IsPrimaryKey=true)]
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        [Column]
        public string ISBN
        {
            get { return _isbn; }
            set
            {
                _isbn = value;
                RaisePropertyChanged(() => ISBN);
            }
        }

        [Column]
        public string SemesterGuid
        {
            get { return _semesterGuid; }
            set
            {
                _semesterGuid = value;
                RaisePropertyChanged(() => SemesterGuid);
            }
        }

        [Column]
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        [Column]
        public string Teahcer
        {
            get { return _teacher; }
            set
            {
                _teacher = value;
                RaisePropertyChanged(() => Teahcer);
            }
        }

        [Column]
        public string Press
        {
            get { return _press; }
            set
            {
                _press = value;
                RaisePropertyChanged(() => Press);
            }
        }

        [Column]
        public double Price
        {
            get { return _price; }
            set
            {
                _price = value;
                RaisePropertyChanged(() => Price);
            }
        }

        [Column]
        public string Image
        {
            get { return _image; }
            set
            {
                _image = value;
                RaisePropertyChanged(() => Image);
                RaisePropertyChanged(() => Stream);
            }
        }

        public Stream Stream
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(Image))
                {
                    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        string fname = Image;
                        if (store.FileExists(fname))
                        {
                            return IsolatedStorageFile.GetUserStoreForApplication().OpenFile(fname, FileMode.Open, FileAccess.Read);
                        }
                    }
                }
                return null;
            }
        }


        public SemesterInfo Semester { get; set; }
        #endregion

        #region Event
        public event EventHandler<PropertyArgs> ValueChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            ValueChanged.Invoke(this, new PropertyArgs()
            {
                Property = GetType().GetProperty(propertyName)
            });
        }
        #endregion
    }
}
